package com.sfh.cp.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cp.R;
import crm.sfh.server.entity.EContent;
import crm.sfh.server.entity.ENote;
import crm.sfh.server.entity.EMessage;

import java.util.List;

/**
 * 发布内容-留言人
 */

public class WidgetContentPersonLayout extends LinearLayout implements View.OnClickListener {



    LinearLayout lyMsg;


    IContentOnClickLayout onClickMessage;
    EContent contentType;
    LinearLayout lyContent;

    public WidgetContentPersonLayout(Context context) {
        super(context);
        this.init();
    }

    public WidgetContentPersonLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public WidgetContentPersonLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }

    private void init() {
        inflate(this.getContext(), R.layout.widget_content_person_layout, this);
        this.lyMsg = this.find(R.id.lyMsg);
        this.lyContent = this.find(R.id.lyContent);
        this.lyMsg.setOnClickListener(this);
    }

    private <T> T find(int r) {
        return (T) findViewById(r);
    }
    public void setOnClickMessage(IContentOnClickLayout onClickMessage) {
        this.onClickMessage = onClickMessage;
    }

    /**
     * 设置数据
     */
    public void setData(EContent person) {
        this.contentType = person;
        if (person.getMessagePerson() == null || person.getMessagePerson().isEmpty()) {
            this.lyContent.setVisibility(GONE);
        } else {
            this.lyContent.setVisibility(VISIBLE);
            this.addTextContent(person.getMessagePerson());
        }

    }


    @Override
    public void onClick(View v) {
        if (onClickMessage == null || contentType == null) {
            return;
        }

        if (v == lyMsg) {
            onClickMessage.onClickMessage(v, contentType.getContentType());
            return;
        }

    }

    private void addTextContent(List<EMessage> personList) {

        lyContent.removeAllViews ();

        if (personList == null || personList.isEmpty ()) {
            return;
        }
        for (EMessage messagePerson : personList) {
            WidgetPersonLayout personLayout = new WidgetPersonLayout (getContext ());
            personLayout.setData (messagePerson);
            lyContent.addView (personLayout);
        }
    }
}
